package ldh.im.fxbase.service;

import ldh.im.dto.business.item.SensitiveWordItem;
import ldh.im.fxbase.dao.ImSensitiveWordDao;
import ldh.im.fxbase.data.pojo.ImSensitiveWord;
import ldh.im.fxbase.data.pojo.status.ImSensitiveWordStatus;
import ldh.im.fxbase.util.BeanFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import toolgood.words.WordsSearchEx2;

import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

public class SensitiveWordService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SensitiveWordService.class);

    private ImSensitiveWordDao sensitiveWordDao = BeanFactory.getInstance().getBean(ImSensitiveWordDao.class);

    private List<ImSensitiveWord> allSensitiveWord = null;
    private List<String> sensitiveWords = null;

    public void saveOrUpdates(String userId, List<SensitiveWordItem> sensitiveWordItems) throws Exception {
        if (sensitiveWordItems.size() < 1) return;

        List<ImSensitiveWord> imSensitiveWords = sensitiveWordItems.stream().map(cinfo->toImSensitiveWord(userId, cinfo)).collect(Collectors.toList());

        Set<ImSensitiveWord> insertImSensitiveWordSet = new LinkedHashSet<>();
        Set<ImSensitiveWord> updateImSensitiveWordSet = new LinkedHashSet<>();

        for(ImSensitiveWord imSensitiveWord : imSensitiveWords) {
            ImSensitiveWord dbSensitiveWord = sensitiveWordDao.getByWord(imSensitiveWord.getWord());
            if (dbSensitiveWord == null) {
                insertImSensitiveWordSet.add(imSensitiveWord);
            } else {
                imSensitiveWord.setId(dbSensitiveWord.getId());
                updateImSensitiveWordSet.add(imSensitiveWord);
            }
        }
        if (insertImSensitiveWordSet.size() > 0) {
            sensitiveWordDao.batchInsert(insertImSensitiveWordSet.stream().collect(Collectors.toList()));
        }
        if (updateImSensitiveWordSet.size() > 0) {
            sensitiveWordDao.batchUpdate(updateImSensitiveWordSet.stream().collect(Collectors.toList()));
        }
    }

    public List<ImSensitiveWord> getAll() throws SQLException {
        if (allSensitiveWord == null) {
            allSensitiveWord = sensitiveWordDao.getAll();
            sensitiveWords = allSensitiveWord.stream().map(word->word.getWord()).collect(Collectors.toList());
        }
        return allSensitiveWord;
    }

    public boolean haveSensitiveWords(String content) {
        try {
            getAll();
        } catch (Exception e) {
            return false;
        }

        if (sensitiveWords.size() < 1) return false;
        List<String> tmpWord = sensitiveWords;
        WordsSearchEx2 sensitiveWords = new WordsSearchEx2();
        sensitiveWords.SetKeywords(tmpWord);

        boolean b = sensitiveWords.ContainsAny(content);
        return b;
    }

    private ImSensitiveWord toImSensitiveWord(String userId, SensitiveWordItem sensitiveWordItem) {
        ImSensitiveWord sensitiveWord = new ImSensitiveWord();
        sensitiveWord.setWord(sensitiveWordItem.getWord());
        sensitiveWord.setStatus(ImSensitiveWordStatus.valueOf(sensitiveWordItem.getStatus().name()));

        return sensitiveWord;
    }
}
